﻿/**
 * Created by Maibenben on 2017/5/4.
 */
$(function () {
    //跳转到本页之后读取cookie中是否有mid和token
    checkCookie();
    var databack = true;
    //清除本地缓存
    localStorage.removeItem("yudate");
    localStorage.removeItem("member_id");
    localStorage.removeItem("desc");
    localStorage.removeItem("base");
    var mdate = new Date();
    var md = mdate.getFullYear() + "-" + (mdate.getMonth() + 1) + "-" + (parseInt(mdate.getDate()) + parseInt(location.search.split("=")[3]));
    localStorage.setItem("yudate", md);
    //没人的时候也可以添加就诊人
    $(".add").on("click", function () {
        window.location.href = "add-person-reference.html" + location.search;
    });
    //进入页面显示就诊人信息
    $.get(hosURL + "?mid=" + $.cookie("mid") + "&token=" + $.cookie("token"), function (res) {
        if (res.total == 0) {
            $(".option").hide();
        } else {
            localStorage.setItem("member_id", res.data[0].id);
            $(".default").text(res.data[0].name);
            $(".people").show();
            var refHTML = template("refTem", res);
            $(".choose_person").html(refHTML);
            //添加就诊人
            if (res.av_add == 1) {
                $(".add").on("click", function () {
                    window.location.href = "add-person-reference.html" + location.search;
                });
            } else {
                $(".add").on("click", function () {
                    creatLayout("就诊人最多只可以添加六个");
                });
            }
            //点击选人
            //var i=0;
            $(".arrow").on("click", function () {
                //i++;
                $(".choose_person").toggle(100);
                //$(this).css("transform","rotate("+180*i+"deg)");
            });
            //点击每个选项
            $(".people_list>li").on("click", function () {
                $(".default").text($(this).text());
                $(".choose_person").hide(100);
                localStorage.setItem("member_id", $(this).attr("data-id"));
            });
        }
    });
    //文本输入300限制
    $('.desc').on('keyup', function () {
        var textval = $(this).val().length;
        if (textval > 300) {
            $('.current').html(300);
        } else {
            $('.current').html(textval);
        }
        if (textval >= 300) {
            var num = $(this).val().substr(0, 300);
            $(this).val(num);
        }
    });
    //上传图片
    var base64 = [];
    $(".inp").on("change", function () {
        var that = $(this).parent();
        var reader = new FileReader;
        if (this.files[0].type.indexOf("image") != -1) {
            reader.readAsDataURL(this.files[0]);
            reader.onload = function () {
                that[0].style.background = "url(" + reader.result + ") no-repeat center/cover";
                that.parent().next().show();
                base64.push(reader.result);
            }

        } else {
            creatLayout("请不要上传其他类型文件");
        }
    });
    //点击下一步进入确认预约
    $(".footer").click(function () {
        if (databack) {
            //判断是否选取了就诊人
            if (localStorage.getItem("member_id")) {
                var member_id = localStorage.getItem("member_id");//就诊人的id
                var desc = $(".desc").val();//就诊人的病情描述
                //这里使用localstorage进行本地保存信息
                localStorage.setItem("desc", desc);
                localStorage.setItem("base", base64);
                databack = false;
                var data = {m_id: member_id, mid: $.cookie("mid"), token: $.cookie("token")};
                $.get(checkStateURL, data, function (res) {
                    databack = true;
                    if (res.state == 1) {
                        window.location.href = "confirm.html";
                    } else if (res.state == 2) {
                        $(".tip").css("display", "block");
                        $(".yes").on("click", function () {
                            window.location.href = "bind-card-confirm.html?id=" + member_id;
                        });
                        $(".no").on("click", function () {
                            window.location.href = "confirm.html";
                        });
                    }
                });
            } else {
                creatLayout("请选取就诊人");
            }
        }
    });
});